﻿using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using Marketplace.Engine.Model;
using Marketplace.Engine.Repository;
using Marketplace.Engine.Repository.Generic;
using Marketplace.Mvc.Models;
using Image = System.Drawing.Image;

namespace Marketplace.Mvc.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to Marketplace";

            return View();
        }

        public ActionResult ToRent()
        {
//            var repository = new RentPropertyRepository();
//            var lastDate = DateTime.Now.AddYears(-1);
//            var listResult = repository.GetList(new ListCondition<RentProperty>(c => c.RegisterationDate > lastDate, null, 20, 0));
//            
//            return View(listResult.Result);
            //var result = new List<RentProperty>();

            return View();
        }

        public ActionResult GetToRentList(RentFilterModel filter)
        {
            var repository = new RentPropertyRepository();
            var lastDate = DateTime.Now.AddYears(-1);
            var listCondition = new ListCondition<RentProperty>(c => c.RegisterationDate > lastDate && c.Price < filter.UpperPrice, null, 20, 0);

            var listResult = repository.GetList(listCondition);

            if (!Request.IsAjaxRequest())
            {
                return View("Index", listResult.Result);
            }

            return PartialView("_RentList", listResult.Result);
        }

        public ActionResult ToRentDetail(int id)
        {
            var repository = new RentPropertyRepository();
            var rentProperty = repository.GetById(p => p.Id == id);
            
            return View(rentProperty);
        }

        public ActionResult ForSale()
        {
            var repository = new SalePropertyRepository();
            var lastDate = DateTime.Now.AddYears(-1);
            var listResult = repository.GetList(new ListCondition<SaleProperty>(c => c.RegisterationDate > lastDate, null, 20, 0));

            return View(listResult.Result);
        }

        public ActionResult ForSaleDetail(int id)
        {
            var repository = new SalePropertyRepository();
            var saleProperty = repository.GetById(p => p.Id == id);

            return View(saleProperty);
        }

        public ActionResult NewHomes()
        {
            return View();            
        }

        public FileStreamResult DefaultImage(int id)
        {
            var image = Image.FromFile(Server.MapPath("~/Content/image.img"));
            var stream = new MemoryStream();
            image.Save(stream, ImageFormat.Jpeg);

            return new FileStreamResult(stream, "image/jpg");
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
